{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "19945317",
   "metadata": {},
   "source": [
    "## 数组与数的计算\n",
    "在python列表中，想要对列表中所有的元素都加一个数，要么采用map函数，要么循环整个列表进行操作。但是Numpy中的数组可以直接在数组上进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "29441814",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e36acc13",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 1, 0, 2],\n",
       "       [2, 9, 8, 4, 3],\n",
       "       [4, 7, 9, 3, 5]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.random.randint(0,10,size=(3,5))\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b5c76daa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  8  2  0  4]\n",
      " [ 4 18 16  8  6]\n",
      " [ 8 14 18  6 10]]\n"
     ]
    }
   ],
   "source": [
    "print(a1*2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd01d62c",
   "metadata": {},
   "source": [
    "相加、相减、相除等也都是类似的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23c88952",
   "metadata": {},
   "source": [
    "## 数组与数组的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d17061fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]\n",
      " [16 17 18 19 20 21 22 23]]\n",
      "[[9 4 4 0 3 1 5 3]\n",
      " [5 5 2 5 7 2 9 9]\n",
      " [4 1 1 2 2 6 7 4]]\n",
      "[[ 9  5  6  3  7  6 11 10]\n",
      " [13 14 12 16 19 15 23 24]\n",
      " [20 18 19 21 22 27 29 27]]\n"
     ]
    }
   ],
   "source": [
    "# 1.结构相同的数组之间的运算\n",
    "a1 = np.arange(0,24).reshape((3,8))\n",
    "a2 = np.random.randint(0,10,size=(3,8))\n",
    "a3 = a1 + a2\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9f161397",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 3 5 9 9 3 9 3]\n",
      " [6 7 2 4 5 9 8 2]\n",
      " [5 1 4 3 0 1 2 6]]\n",
      "[[9]\n",
      " [6]\n",
      " [9]]\n",
      "[[ 6  6  4  0  0  6  0  6]\n",
      " [ 0 -1  4  2  1 -3 -2  4]\n",
      " [ 4  8  5  6  9  8  7  3]]\n"
     ]
    }
   ],
   "source": [
    "# 2.与行数相同并且只有一列的数组之间的运算\n",
    "a1 = np.random.randint(0,10,size=(3,8))\n",
    "a2 = np.random.randint(0,10,size=(3,1))\n",
    "a3 = a2-a1\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5d45fec0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4 2 5 8 3 2 1]\n",
      " [3 8 1 5 3 3 2 8]\n",
      " [7 8 0 8 0 0 4 5]]\n",
      "[[1 3 7 7 9 1 0 4]]\n",
      "[[ 3  7  9 12 17  4  2  5]\n",
      " [ 4 11  8 12 12  4  2 12]\n",
      " [ 8 11  7 15  9  1  4  9]]\n"
     ]
    }
   ],
   "source": [
    "# 3.与列数相同并且只有1行的数组之间的运算\n",
    "a1 = np.random.randint(0,10,size=(3,8))\n",
    "a2 = np.random.randint(0,10,size=(1,8))\n",
    "a3 = a1 + a2\n",
    "print(a1)\n",
    "print(a2)\n",
    "print(a3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ac1293a",
   "metadata": {},
   "source": [
    "## 广播原则\n",
    "如果两个数组的后缘难度（即从末尾开始算起的维度）的轴长度相符或者其中一方发长度为1，则认为他们是广播兼容的。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af7c446",
   "metadata": {},
   "source": [
    "# 总结：\n",
    "1.数组和数字直接进行运算是没有问题的  \n",
    "2.两个shape相同的数组是可以进行运算的。  \n",
    "3.如果两个shape不同的数组，想要进行运算，那么要看他们是否满足广播原则。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
